#pragma once
#include <iostream>
#include <iostream>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <fcntl.h>
#include <sys/shm.h>

using namespace std;

#define PATHNAME "."
#define PROJID 0X6666
#define NUM 1024

const std::string fifoname = "./fifo";
const std::string fifoname1 = "./fifo1";
uint32_t mode = 0666;

const int gsize = 4096;
key_t getKey(){
    key_t k = ftok(PATHNAME,PROJID);
    if(k==-1){
        cerr<<"error:"<<errno<<":"<<strerror(errno)<<endl;
        exit(1);
    }
    return k;
}

static int createShmHelper(key_t k,int size,int flag)
{
    int shmid = shmget(k,gsize,flag);
    if(shmid==-1){
        cerr<<"error:"<<errno<<":"<<strerror(errno)<<endl;
        exit(2);
    }
    return shmid;
}

int createShm(key_t k,int size){
    umask(0);
    return createShmHelper(k,size,IPC_CREAT|IPC_EXCL|0666);
}

int getShm(key_t k,int size){
    return createShmHelper(k,size,IPC_CREAT);
}

int my_open(const char* file,int flag){
    int fd = open(file,flag);
    if(fd<0){
        cout<<errno<<":"<<strerror(errno)<<endl;
        return 2;
    }
    return fd;
}
void my_mkfifo(const char* file,int flag){
    umask(0);
    int n = mkfifo(file,flag);
    if(n!=0){
        cout<<errno<<":"<<strerror(errno)<<endl;
    }
}